package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.utils.to.MemberPrice;
import com.atguigu.common.utils.to.SkuReductionTo;
import com.atguigu.gulimall.coupon.entity.SmsMemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SmsSkuLadderEntity;
import com.atguigu.gulimall.coupon.service.SmsMemberPriceService;
import com.atguigu.gulimall.coupon.service.SmsSkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SmsSkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SmsSkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SmsSkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {
    @Autowired
    private SmsSkuLadderService smsSkuLadderService;
    @Autowired
    private SmsSkuFullReductionService  smsSkuFullReductionService;
    @Autowired
    private SmsMemberPriceService  smsMemberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //1、往sms_sku_ladder保存数据
        SmsSkuLadderEntity skuLadderEntity = new SmsSkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
        if(skuReductionTo.getFullCount() > 0){
            smsSkuLadderService.save(skuLadderEntity);
        }


        //2、往sms_sku_full_reduction保存数据
        SmsSkuFullReductionEntity reductionEntity = new SmsSkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,reductionEntity);
        if(skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            this.save(reductionEntity);
        }


        //3、往sms_member_price保存数据
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<SmsMemberPriceEntity> collect = memberPrice.stream().map(item -> {
            SmsMemberPriceEntity priceEntity = new SmsMemberPriceEntity();
            priceEntity.setSkuId(skuReductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1);
            return priceEntity;
        }).filter(item ->{
           return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());

        smsMemberPriceService.saveBatch(collect);

    }

}